package com.yang.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.yang.entity.Fiel;
import com.yang.util.DBHelp;
import com.yang.util.MapRow;

public class FielDao {

	Fiel f = new Fiel();
	DBHelp<Fiel> dh = new DBHelp<Fiel>();

	public boolean insert(Fiel f) throws SQLException {
		String sql = "insert into t_file(name,contentType,roundName,length,`time`,father,son,md,userid)value(?,?,?,?,?,?,?,?,?)";
		return dh.add(sql, f.getName(), f.getContentType(), f.getRoundName(),
				f.getLength(), f.getTime(), f.getFather(), f.getSon(),f.getMd(),f.getUserid());
	}

	public Fiel getFile(int fileId) {
		String sql = "select * from t_file where id=?";
		MapRow<Fiel> mr = new MR();
		return dh.findObj(sql, mr, fileId);
	}
	public boolean deleteAllData(){
		String sql="delete from t_file";
		return dh.add(sql);
	}

	public List<Fiel> getList( int father) {
		String sql = "select * from t_file where father=?";
		MapRow<Fiel> mr = new MR();
		return dh.findList(sql, mr,father);
	}
	public boolean update(int father,String roundName){
		String sql="UPDATE t_file SET son=(SELECT id FROM t_file WHERE roundName=?) where id=?";
		return dh.add(sql,roundName,father);
	}

	private class MR implements MapRow<Fiel> {

		@Override
		public Fiel RowMap(ResultSet rs) throws SQLException {
			Fiel f = new Fiel();
			f.setId(rs.getInt("id"));
			f.setContentType(rs.getString("contentType"));
			f.setLength(rs.getLong("length"));
			f.setName(rs.getString("name"));
			f.setRoundName(rs.getString("roundName"));
			f.setMd(rs.getString("md"));
			f.setTime(rs.getString("time"));
			f.setFather(rs.getInt("father"));
			f.setSon(rs.getInt("son"));
			f.setUserid(rs.getInt("userid"));
			return f;
		}

	}

	public List<Fiel> getListSon(int son) {
		String sql = "select * from t_file where son=?";
		MapRow<Fiel> mr = new MR();
		return dh.findList(sql, mr,son);
	}

	public List<Fiel> findByMDs(String md5) {
		String sql = "select * from t_file where md=?";
		MapRow<Fiel> mr = new MR();
		return dh.findList(sql, mr,md5);
	}
	public Fiel findByMD5(String md5) {
		String sql = "select * from t_file where md=?";
		MapRow<Fiel> mr = new MR();
		return dh.findObj(sql, mr,md5);
	}

	public boolean newName(int id, String name) {
		String sql="update t_file set name=? where id=?";
		return dh.add(sql, name,id);
	}

	public boolean update(Fiel fielfather) {
		String sql="update t_file set son=?,`length`=? where id=?";
		return dh.add(sql, fielfather.getSon(),fielfather.getLength(),fielfather.getId());
	}

	public boolean update(int id,int visiable) {
		String sql="update t_file set visiable=?  where id=?";
		return dh.add(sql,visiable,id);
	}

	public boolean delFile(int id) {
		String sql="delete from t_file where id=?";
		return dh.add(sql,id);
	}

}
